package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpServletRequest request) {
        Object user = request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, user);
        return R.success(addressBookService.list(lqw));
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody AddressBook addressBook) {
        Object user = request.getSession().getAttribute("user");
        addressBook.setUserId((Long) user);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    //http://localhost:8080/addressBook/default
    @PutMapping("/default")
    public R<AddressBook> setDefault(HttpServletRequest request, @RequestBody AddressBook addressBook) {
        Object user = request.getSession().getAttribute("user");
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getUserId, user);
        lqw.set(AddressBook::getIsDefault, 0);
        addressBookService.update(lqw);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    //地址回显
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpServletRequest request) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, request.getSession().getAttribute("user"));
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (null == addressBook) {
            return R.error("没有设置默认地址");
        } else {
            return R.success(addressBook);
        }
    }

    //http://localhost:8080/addressBook?ids=1586269077895647233
    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
